package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.dao.AccountDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }




    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = request.getParameter("username");
        System.out.println(user);
        String password = request.getParameter("password");
        System.out.println(password);
        // 链接数据库
        SqlSession sqlSession = DBOperation.getSqlSession();
        //创建AccountDao
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);



        //判断是邮箱还是手机号的正则表达式
        String em = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        String ph = "1(3|4|5|6|7|8|9)\\d{9}$";

        //新建一个账户判断用实例
        Account obj = new Account();
        //
        if(user.matches(ph)) { //phoneNumber
            obj.setPhone(user);
        } else if(user.matches(em)) { // email
            obj.setEmail(user);
        } else { //username
            obj.setUsername(user);
        }
        System.out.println(obj);
        //从数据库中搜索该用户
        Account account = accountDao.findOneByAccount(obj);

        //如果没有该用户
        if(account == null) {
            System.out.println("user = null");
            //提示“用户不存在”
            request.setAttribute("loginState", "userNull");
            System.out.println(request.getAttribute("loginState"));
            //转到登录页面
            request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
        } else { //存在
            if(account.getPassword().equals(password)) { //密码匹配成功，登录
                System.out.println("user login success");
                //保存当前登录用户信息
                request.getSession().setAttribute("curUser", account);
                //判断Session是否存在上一页地址信息，如果pageUpURI存在非null值，则构造地址并跳转
                if (request.getSession().getAttribute("pageUpURI")!=null){
                    //获取当前页面id
                    int id = 0;
                    String pageUpURI = (String) request.getSession().getAttribute("pageUpURI");
                    String hotelName = pageUpURI.split("/")[3].replace("order","").replace("Hotel.jsp", "");
                    switch (hotelName) {
                        case "bocui":
                            id = 1;
                            break;
                        case "jianguo":
                            id=2;
                            break;
                        case "nanyuan":
                            id = 3;
                            break;
                        case "jinglun":
                            id = 4;
                            break;
                    }
                    //构造跳转地址
                    String URI = "OrderHotelServlet?id=" + id;
                    //清除pageUpURI
                    request.getSession().setAttribute("pageUpURI",null);
                    //跳转
                    request.getRequestDispatcher(URI).forward(request,response);
                }else {
                    System.out.println("to index");
                    //如果pageUpURI为null，则跳转到index
                    request.getRequestDispatcher("index.jsp").forward(request,response);
                }
            } else { //密码匹配失败，登录失败
                System.out.println("user login failed");
                //保留本次输入的账号
                request.setAttribute("username", user);
                //提示“密码错误”
                request.setAttribute("loginState", "pwdError");
                //转到登录
                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
            }
        }

//
//        if(user.matches(ph)) {//手机号登录
//            obj.setPhone(user);
//            System.out.println(obj);
//            Account account = accountDao.findByPhone(obj);
//            System.out.println(account);
//            if (account == null) {
//                System.out.println("手机号不存在！");

//            } else if (account.getPassword().equals(password)) {
//                System.out.println("手机号登陆成功");
//                request.getSession().setAttribute("curUser", account);
//                request.getRequestDispatcher("index.jsp").forward(request, response);

//            } else {
//                System.out.println(user);
//                System.out.println(password);
//                System.out.println("手机号和密码不匹配！");
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);

//            }
//
//        }else if(user.matches(em)){//邮箱登录
//            obj.setEmail(user);
//            Account account = accountDao.findByEmail(obj);
//            System.out.println(account);
//            if (account == null) {
//                System.out.println("邮箱不存在！");
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
//            } else if (account.getPassword().equals(password)) {
//                System.out.println("邮箱登陆成功");
//                request.getSession().setAttribute("curUser", account);
//                request.getRequestDispatcher("index.jsp").forward(request, response);

//            } else {
//                System.out.println(user);
//                System.out.println(password);
//                System.out.println("邮箱账号和密码不匹配！");
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);

//            }
//        }else {//用户名登录
//            obj.setUsername(user);
//            Account account = accountDao.findByName(obj);
//            System.out.println(account);
//            if (account == null) {
//                System.out.println("用户名不存在！");
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);

//            } else if (account.getPassword().equals(password)) {
//                System.out.println("用户名登陆成功");
//                request.getSession().setAttribute("curUser", account);
//                request.getRequestDispatcher("index.jsp").forward(request, response);

//            } else {
//                System.out.println(user);
//                System.out.println(password);
//                System.out.println("用户名和密码不匹配！");
//                request.getRequestDispatcher("jsp/login.jsp").forward(request, response);

//            }
//        }

        //关闭数据库链接
        sqlSession.close();
    }
}
